package com.tao.dao;

import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class BaseDao {
    protected Connection conn = null;
    protected Statement stmt = null;
    protected ResultSet rs = null;
//    protected String url = "jdbc:mysql:///qfnu?characterEncoding=utf-8";
//    protected String url = "jdbc:mysql:///qfnu?serverTimezone=UTC";
    protected String url = "jdbc:mysql:///qfnu?serverTimezone=UTC&characterEncoding=UTF-8";
    protected String name = "root";
//    protected String password = "zm1712491149";
    protected String password = "afanda";
    private String jdbcName = "com.mysql.cj.jdbc.Driver";
    @Autowired
    protected PreparedStatement pstmt=null;

    public void connect() {
        try {
            Class.forName(jdbcName);
            conn = DriverManager.getConnection(url, name, password);
            stmt = conn.createStatement();
            System.out.println("链接成功");
        } catch(Exception e) {
            e.printStackTrace();
            System.out.println("连接失败");
        }
    }

    public void closeAll(){
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
            if(pstmt!=null){
                pstmt.close();
            }
        }catch(Exception e) {

        }
    }
}


